﻿using System;
using System.Data;
using System.Text;
using MySql.Data.MySqlClient;
using Maticsoft.DBUtility;//Please add references
namespace Maticsoft.DAL
{
    /// <summary>
    /// 数据访问类:tb_userdata
    /// </summary>
    public partial class tb_userdata
    {
        public tb_userdata()
        { }
        #region  BasicMethod
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from tb_userdata");
            strSql.Append(" where id=@id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@id", MySqlDbType.Int32)
			};
            parameters[0].Value = id;

            return DbHelperMySQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Maticsoft.Model.tb_userdata model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into tb_userdata(");
            strSql.Append("realname,imgurl,qrpath,wxid,jifen,freezejifen,tj_num,gz_time,qrmediaid,qr_expire_date)");
            strSql.Append(" values (");
            strSql.Append("@realname,@imgurl,@qrpath,@wxid,@jifen,@freezejifen,@tj_num,@gz_time,@qrmediaid,@qr_expire_date)");
            MySqlParameter[] parameters = {
					new MySqlParameter("@realname", MySqlDbType.VarChar,255),
					new MySqlParameter("@imgurl", MySqlDbType.VarChar,255),
					new MySqlParameter("@qrpath", MySqlDbType.VarChar,255),
					new MySqlParameter("@wxid", MySqlDbType.VarChar,255),
					new MySqlParameter("@jifen", MySqlDbType.Decimal,10),
					new MySqlParameter("@freezejifen", MySqlDbType.Decimal,10),
					new MySqlParameter("@tj_num", MySqlDbType.Int32,11),
					new MySqlParameter("@gz_time", MySqlDbType.DateTime),
					new MySqlParameter("@qrmediaid", MySqlDbType.VarChar,255),
					new MySqlParameter("@qr_expire_date", MySqlDbType.DateTime)};
            parameters[0].Value = model.realname;
            parameters[1].Value = model.imgurl;
            parameters[2].Value = model.qrpath;
            parameters[3].Value = model.wxid;
            parameters[4].Value = model.jifen;
            parameters[5].Value = model.freezejifen;
            parameters[6].Value = model.tj_num;
            parameters[7].Value = model.gz_time;
            parameters[8].Value = model.qrmediaid;
            parameters[9].Value = model.qr_expire_date;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Maticsoft.Model.tb_userdata model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tb_userdata set ");
            strSql.Append("realname=@realname,");
            strSql.Append("imgurl=@imgurl,");
            strSql.Append("qrpath=@qrpath,");
            strSql.Append("wxid=@wxid,");
            strSql.Append("jifen=@jifen,");
            strSql.Append("freezejifen=@freezejifen,");
            strSql.Append("tj_num=@tj_num,");
            strSql.Append("gz_time=@gz_time,");
            strSql.Append("qrmediaid=@qrmediaid,");
            strSql.Append("qr_expire_date=@qr_expire_date");
            strSql.Append(" where id=@id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@realname", MySqlDbType.VarChar,255),
					new MySqlParameter("@imgurl", MySqlDbType.VarChar,255),
					new MySqlParameter("@qrpath", MySqlDbType.VarChar,255),
					new MySqlParameter("@wxid", MySqlDbType.VarChar,255),
					new MySqlParameter("@jifen", MySqlDbType.Decimal,10),
					new MySqlParameter("@freezejifen", MySqlDbType.Decimal,10),
					new MySqlParameter("@tj_num", MySqlDbType.Int32,11),
					new MySqlParameter("@gz_time", MySqlDbType.DateTime),
					new MySqlParameter("@qrmediaid", MySqlDbType.VarChar,255),
					new MySqlParameter("@qr_expire_date", MySqlDbType.DateTime),
					new MySqlParameter("@id", MySqlDbType.Int32,11)};
            parameters[0].Value = model.realname;
            parameters[1].Value = model.imgurl;
            parameters[2].Value = model.qrpath;
            parameters[3].Value = model.wxid;
            parameters[4].Value = model.jifen;
            parameters[5].Value = model.freezejifen;
            parameters[6].Value = model.tj_num;
            parameters[7].Value = model.gz_time;
            parameters[8].Value = model.qrmediaid;
            parameters[9].Value = model.qr_expire_date;
            parameters[10].Value = model.id;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tb_userdata ");
            strSql.Append(" where id=@id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@id", MySqlDbType.Int32)
			};
            parameters[0].Value = id;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tb_userdata ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Maticsoft.Model.tb_userdata GetModel(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,realname,imgurl,qrpath,wxid,jifen,freezejifen,tj_num,gz_time,qrmediaid,qr_expire_date from tb_userdata ");
            strSql.Append(" where id=@id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@id", MySqlDbType.Int32)
			};
            parameters[0].Value = id;

            Maticsoft.Model.tb_userdata model = new Maticsoft.Model.tb_userdata();
            DataSet ds = DbHelperMySQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Maticsoft.Model.tb_userdata DataRowToModel(DataRow row)
        {
            Maticsoft.Model.tb_userdata model = new Maticsoft.Model.tb_userdata();
            if (row != null)
            {
                if (row["id"] != null && row["id"].ToString() != "")
                {
                    model.id = int.Parse(row["id"].ToString());
                }
                if (row["realname"] != null)
                {
                    model.realname = row["realname"].ToString();
                }
                if (row["imgurl"] != null)
                {
                    model.imgurl = row["imgurl"].ToString();
                }
                if (row["qrpath"] != null)
                {
                    model.qrpath = row["qrpath"].ToString();
                }
                if (row["wxid"] != null)
                {
                    model.wxid = row["wxid"].ToString();
                }
                if (row["jifen"] != null && row["jifen"].ToString() != "")
                {
                    model.jifen = decimal.Parse(row["jifen"].ToString());
                }
                if (row["freezejifen"] != null && row["freezejifen"].ToString() != "")
                {
                    model.freezejifen = decimal.Parse(row["freezejifen"].ToString());
                }
                if (row["tj_num"] != null && row["tj_num"].ToString() != "")
                {
                    model.tj_num = int.Parse(row["tj_num"].ToString());
                }
                if (row["gz_time"] != null && row["gz_time"].ToString() != "")
                {
                    model.gz_time = DateTime.Parse(row["gz_time"].ToString());
                }
                if (row["qrmediaid"] != null)
                {
                    model.qrmediaid = row["qrmediaid"].ToString();
                }
                if (row["qr_expire_date"] != null && row["qr_expire_date"].ToString() != "")
                {
                    model.qr_expire_date = DateTime.Parse(row["qr_expire_date"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,realname,imgurl,qrpath,wxid,jifen,freezejifen,tj_num,gz_time,qrmediaid,qr_expire_date ");
            strSql.Append(" FROM tb_userdata ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperMySQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM tb_userdata ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperMySQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        //public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        //{
        //    StringBuilder strSql = new StringBuilder();
        //    strSql.Append("SELECT * FROM ( ");
        //    strSql.Append(" SELECT ROW_NUMBER() OVER (");
        //    if (!string.IsNullOrEmpty(orderby.Trim()))
        //    {
        //        strSql.Append("order by T." + orderby);
        //    }
        //    else
        //    {
        //        strSql.Append("order by T.id desc");
        //    }
        //    strSql.Append(")AS Row, T.*  from tb_userdata T ");
        //    if (!string.IsNullOrEmpty(strWhere.Trim()))
        //    {
        //        strSql.Append(" WHERE " + strWhere);
        //    }
        //    strSql.Append(" ) TT");
        //    strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
        //    return DbHelperMySQL.Query(strSql.ToString());
        //}

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            MySqlParameter[] parameters = {
                    new MySqlParameter("@tblName", MySqlDbType.VarChar, 255),
                    new MySqlParameter("@fldName", MySqlDbType.VarChar, 255),
                    new MySqlParameter("@PageSize", MySqlDbType.Int32),
                    new MySqlParameter("@PageIndex", MySqlDbType.Int32),
                    new MySqlParameter("@IsReCount", MySqlDbType.Bit),
                    new MySqlParameter("@OrderType", MySqlDbType.Bit),
                    new MySqlParameter("@strWhere", MySqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "tb_userdata";
            parameters[1].Value = "id";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperMySQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  BasicMethod
        #region  ExtensionMethod
        public DataSet GetListByPage(string strWhere, string orderby, int pageSize, int pageNo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM  tb_userdata ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by " + orderby + " ");
            }
            else
            {
                strSql.Append("order by id desc ");
            }
            strSql.Append(" limit ");
            strSql.Append(((pageNo - 1) * pageSize).ToString() + ",");
            strSql.Append(pageSize.ToString());
            return DbHelperMySQL.Query(strSql.ToString());
        }
        #endregion  ExtensionMethod
    }
}

